package com.gmail.huxiaowi.cblog.dao.impl.jdo;

import java.util.Collection;

import javax.jdo.JDOException;
import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import org.springframework.orm.jdo.JdoCallback;

import com.gmail.huxiaowi.cblog.dao.ICategoryDao;
import com.gmail.huxiaowi.cblog.entity.Category;

/**
 * Class CategoryDaoJDOImpl
 * 
 * @author  huxw
 * @version $Revision:1.0.0, $Date:2010-4-29 下午05:45:57$
 */
public class CategoryDaoJDOImpl extends GenericDaoJDOImpl<Category> implements
        ICategoryDao {

    /* (non-Javadoc)
     * @see com.gmail.huxiaowi.cblog.dao.CategoryDao#queryByUser(com.gmail.huxiaowi.cblog.entity.User)
     */
    @SuppressWarnings({ "unchecked", "rawtypes" })
    public Collection<Category> queryAll() {
        // TODO Auto-generated method stub
        return jt.executeFind(new JdoCallback() {
            
            public Object doInJdo(PersistenceManager persistencemanager)
                    throws JDOException {
                Query q = persistencemanager.newQuery("SELECT FROM com.gmail.huxiaowi.cblog.entity.Category");
                Collection<Category> catagories = (Collection<Category>) q.execute();
                persistencemanager.detachCopyAll(catagories);
                return catagories;
            }
        });
    }

    public Category queryByName(String name) {
        return (Category) jt.find("SELECT FROM com.gmail.huxiaowi.cblog.entity.Category WHERE name==" + name).iterator().next();
    }

    @Override
    public void renameCategory(Long id, String newName) {
    }
}
